package com.beimihu.until;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.*;

public class ExcelV2 {
    public static void readExcel(String path){

        try {
            FileInputStream fis = new FileInputStream(path);

            Workbook workbook = new XSSFWorkbook(fis);

            Sheet sheet = workbook.getSheetAt(0);
            List<Map<String, Object>> allRowsData = new ArrayList<>();

            for(int i = 2; i <= sheet.getLastRowNum()  ; i++){
                Row row = sheet.getRow(i);
                Map<String, Object> rowData = new LinkedHashMap<>();

                if(row !=null){
                    Cell id = row.getCell(0);
                    Cell name = row.getCell(1);
                    Cell life = row.getCell(2);
                    Cell type = row.getCell(3);
                    Cell prop_1 = row.getCell(4);
                    Cell point_1 = row.getCell(5);
                    Cell type_numb_1 = row.getCell(6);
                    Cell prop_numb_1 = row.getCell(7);
                    Cell prop_2 = row.getCell(8);
                    Cell point_2 = row.getCell(9);
                    Cell type_numb_2 = row.getCell(10);
                    Cell prop_numb_2 = row.getCell(11);
                    Cell prop_3 = row.getCell(12);
                    Cell point_3 = row.getCell(13);
                    Cell type_numb_3 = row.getCell(14);
                    Cell prop_numb_3 = row.getCell(15);
                    Cell prefab = row.getCell(16);
                    Cell skill = row.getCell(17);


                    rowData.put("id", getCellToString(id));
                    rowData.put("name", getCellToString(name));
                    rowData.put("life", getCellToString(life));
                    rowData.put("type", getCellToString(type));
                    rowData.put("prop_1", getCellToString(prop_1));
                    rowData.put("point_1", getCellToString(point_1));
                    rowData.put("type_numb_1", getCellToString(type_numb_1));
                    rowData.put("prop_numb_1", getCellToString(prop_numb_1));
                    rowData.put("prop_2", getCellToString(prop_2));
                    rowData.put("point_2", getCellToString(point_2));
                    rowData.put("type_numb_2", getCellToString(type_numb_2));
                    rowData.put("prop_numb_2", getCellToString(prop_numb_2));
                    rowData.put("prop_3", getCellToString(prop_3));
                    rowData.put("point_3", getCellToString(point_3));
                    rowData.put("type_numb_3", getCellToString(type_numb_3));
                    rowData.put("prop_numb_3", getCellToString(prop_numb_3));
                    rowData.put("prefab", getCellToString(prefab));
                    rowData.put("skill", getCellToString(skill));

                    allRowsData.add(rowData);
                }
            }

            // 创建 Gson 对象，设置为格式化输出
            Gson gson = new GsonBuilder()
                    .setPrettyPrinting()
                    .serializeNulls()
                    .create();
            // 将所有行数据列表转换为 JSON 字符串
            String jsonOutput = gson.toJson(allRowsData);

            // 打印最终的 JSON 输出
            System.out.println(jsonOutput);

            // 关闭工作簿
            workbook.close();


        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }


    }

    public static Object getCellToString(Cell cell) {
        if (cell == null) {
            return null;
        }

        switch (cell.getCellType()) {
            case STRING:
                return cell.getStringCellValue();
            case NUMERIC:
                if (DateUtil.isCellDateFormatted(cell)) {
                    // 如果是日期格式，可根据需求进一步处理日期
                    return cell.getDateCellValue().toString();
                } else {
                    double numericValue = cell.getNumericCellValue();
                    if (numericValue == (long) numericValue) {
                        // 如果是整数，转换为 long 类型
                        return (long) numericValue;
                    } else {
                        // 否则按普通小数处理
                        return numericValue;
                    }
                }
            case BOOLEAN:
                return cell.getBooleanCellValue();
            case FORMULA:
                // 处理公式单元格，重新计算并获取结果
                FormulaEvaluator evaluator = cell.getSheet().getWorkbook().getCreationHelper().createFormulaEvaluator();
                CellValue cellValue = evaluator.evaluate(cell);
                return getCellValueFromCellValue(cellValue);
            default:
                return "";
        }
    }

    // 根据 CellValue 获取合适的值
    private static Object getCellValueFromCellValue(CellValue cellValue) {
        switch (cellValue.getCellType()) {
            case STRING:
                return cellValue.getStringValue();
            case NUMERIC:
                double numericValue = cellValue.getNumberValue();
                if (numericValue == (long) numericValue) {
                    return (long) numericValue;
                } else {
                    return numericValue;
                }
            case BOOLEAN:
                return cellValue.getBooleanValue();
            default:
                return "";
        }
    }

    public static void main(String[] args) {
        String filePath = "./场景掉落物品道具表.xlsx"; // Excel 文件的路径
        readExcel(filePath);
    }
}
